import numpy as np
import csv
import random
from datetime import datetime
from time import strftime


def getCurTime():
    """
    get current time
    Return value of the date string format(%Y-%m-%d %H:%M:%S)
    """
    format='%Y-%m-%d %H:%M:%S'
    sdate = None
    cdate = datetime.now()
    try:
        sdate = cdate.strftime(format)
    except:
        raise ValueError
    return sdate


def multi_unique(inputList):
    str_list = np.array([])
    for item in inputList:
        temp_str = ""
        for x in item:
            temp_str += ";" + str(x)
        str_list = np.append(str_list, temp_str)
    #print str_list
    uni_str = np.unique(str_list)
    uni_float = np.array([])
    #print uni_str
    for item in uni_str:
        temp_item = item[1:]
        #print temp_item
        #break
        temp_list = temp_item.split(";")
        for temp_list_item in temp_list:
            uni_float = np.append(uni_float, float(temp_list_item))
    iLenInputList = inputList.shape
    uni_float.shape = (-1, iLenInputList[1])
    return uni_float

def build_data_list(inputCSV):
    sKey = np.array([])
    fn = inputCSV
    ra = csv.DictReader(file(fn), dialect="excel")
    for record in ra:
        #print record[ra.fieldnames[0]], type(record[ra.fieldnames[-1]])
        for item in ra.fieldnames:
            temp = float(record[item])
            sKey =np.append(sKey, temp)
    sKey.shape=(-1,len(ra.fieldnames))
    return sKey


#--------------------------------------------------------------------------
#MAIN

if __name__ == "__main__":
    print "begin at " + getCurTime()
    
    #unitCSV = "C:/Documents and Settings/wang322/My Documents/Downloads/WelFlows_3.csv"
    unitCSV = "D:\My Documents\My Dropbox/WelFlows_3.csv"
    dataMatrix = build_data_list(unitCSV)
    iLen = dataMatrix.shape
    #coorCSV = "C:/_DATA/Flow/pt.csv"

    temp = multi_unique(dataMatrix[:,4:6])
    i = 0
    temp_deleteList = np.array([])
    for item in temp:
        if (item[0] < 0) or (item[1] < 0):
            temp_deleteList = np.append(temp_deleteList, i)
        i += 1
    temp_deleteList = temp_deleteList[np.argsort(-temp_deleteList[:]),:]
    #print temp_deleteList
    for item in temp_deleteList:
        temp = np.delete(temp, int(item), 0)
    
    iLenTemp = temp.shape    
    coordinate = np.zeros((iLenTemp[0],iLenTemp[1]+1))
    coordinate[:,1:3] = temp
    i = 0
    for item in coordinate:
        item[0] = i
        i += 1
    print coordinate

    unit_attri = np.zeros((iLen[0],iLen[1]+1))
    unit_attri[:,0:-1] = dataMatrix
    #print unit_attri
    
    for item in unit_attri:
        flag = 0
        for coor in coordinate:
            if(item[4] == coor[1]) & (item[5] == coor[2]):
                item[-1] = coor[0]
                flag = 1
                break
        if flag == 0:
            item[-1] = -1

    i = 0
    temp_deleteList = np.array([])
    for item in unit_attri:
        if int(item[-1]) == -1:
            temp_deleteList = np.append(temp_deleteList, i)
        i += 1
    temp_deleteList = temp_deleteList[np.argsort(-temp_deleteList[:]),:]
    for item in temp_deleteList:
        unit_attri = np.delete(unit_attri, int(item), 0)
        
    #print unit_attri[0]
    flowMatrix = np.array([])
    iLen = unit_attri.shape
    i = 0            
    for item in unit_attri:
        if (i+1) < iLen[0]:
            if unit_attri[i+1,0] == item[0]:
                flowMatrix = np.append(flowMatrix, [item[0], item[-1], unit_attri[i+1,-1], item[8], item[9]])
        i += 1
    flowMatrix.shape = (-1, 5)
    #print flowMatrix
    output = np.array([])
    uni_flow = multi_unique(flowMatrix[:,1:3])
    iLen = flowMatrix.shape
    #print flowMatrix[0]
    for i_flow in uni_flow:
        temp_data = np.array([])
        for i_data in flowMatrix:
            if (i_flow[0] == i_data[1]) & (i_flow[1] == i_data[2]):
                temp_data = np.append(temp_data, i_data)
        temp_data.shape = (-1, iLen[1])
        iLenTemp = temp_data.shape
        output = np.append(output, [i_flow[0], i_flow[1], iLenTemp[0]])
        for i in range(3,5):
            temp_value = temp_data[:,i]
            temp = [np.max(temp_value), np.min(temp_value), np.mean(temp_value), np.max(temp_value) - np.min(temp_value)]
            output = np.append(output, temp)

    output.shape = (-1, 11)
    #unit_attri
    filePath = unitCSV[:-4] + "_ID.csv"
    np.savetxt(filePath, unit_attri, delimiter=',')
    filePath = unitCSV[:-4] + "_UniCoor.csv"
    np.savetxt(filePath, coordinate, delimiter=',')
    filePath = unitCSV[:-4] + "_uni_flow.csv"
    np.savetxt(filePath, uni_flow, delimiter=',')
    filePath = unitCSV[:-4] + "_flowMatrix.csv"
    np.savetxt(filePath, flowMatrix, delimiter=',')
    filePath = unitCSV[:-4] + "_FlowCount.csv"
    np.savetxt(filePath, output, delimiter=',')  

    print "end at " + getCurTime()
    print "========================================================================"  

           
